package cn.edu.scau.cmi.shishuai.hibernate.jfx;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.shishuai.hibernate.DAO.StudentDAO;
import cn.edu.scau.cmi.shishuai.hibernate.DAO.TeacherDAO;
import cn.edu.scau.cmi.shishuai.hibernate.domain.Student;
import cn.edu.scau.cmi.shishuai.hibernate.domain.Teacher;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;


public class UI implements Initializable {
	private StudentDAO std;
	private TeacherDAO tea;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		std = new StudentDAO();
		tea = new TeacherDAO();
		log.setEditable(false);
	}
	
	@FXML
	private Button addStudent;
	@FXML
	private Button addTeacher;
	@FXML
	private Button modStudent;
	@FXML
	private Button modTeacher;
	@FXML
	private Button SelStudent;
	@FXML
	private Button delStudent;
	@FXML
	private Button SelTeacher;
	@FXML
	private Button delTeacher;
	@FXML
	private Button TeaModStu;
	@FXML
	private Button StuModTea;
	@FXML
	private TextArea log;


	
	private String getAttr(String name, String title) throws Exception {
		TextInputDialog dialog = new TextInputDialog();
		dialog.setTitle(title);
		dialog.setHeaderText(null);
		dialog.setContentText(name);
		Optional<String> result = dialog.showAndWait();
		if (!result.isPresent()) {
			throw new Exception("null");
		}
		return result.get();
	}
	
	@FXML
	public void onAddStudent(ActionEvent av) throws Exception {
		Student student = new Student();
		try {
			student.setName(getAttr("学生名字", "添加学生"));
			student.setId(Long.parseLong(getAttr("学生id", "添加学生")));
			std.save(student);
			log.appendText("添加学生：" + student.getName() + "成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("添加失败\n");
		}
	}
	@FXML
	public void onAddTeacher(ActionEvent av) throws Exception {
		Teacher student = new Teacher();
		try {
			student.setName(getAttr("老师名字", "添加老师"));
			student.setId(Long.parseLong(getAttr("老师id", "添加老师")));
			tea.save(student);
			log.appendText("添加老师：" + student.getName() + "成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("添加失败\n");
		}
	}
	@FXML
	public void onModStudent(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "修改学生"));
			Student stu = std.getById(id);
			stu.setName(getAttr("学生名字", "修改学生"));
			std.update(stu);
			log.appendText("修改成功\n");
		} catch (Exception e) {
			// TODO: handle exception
			log.appendText("修改失败\n");
		}
	}
	@FXML
	public void onModTeacher(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("老师id", "修改老师"));
			Teacher stu = tea.getById(id);
			stu.setName(getAttr("老师名字", "修改老师"));
			tea.update(stu);
			log.appendText("修改成功\n");
		} catch (Exception e) {
			// TODO: handle exception
			log.appendText("修改失败\n");
		}
	}
	@FXML
	public void onDelStudent(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "删除学生"));
			std.delete(new Student(id));
			log.appendText("删除成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("删除失败\n");
		}
	}
	@FXML
	public void onSelTeacher(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "学生选择指导老师"));
			Long tId = Long.parseLong(getAttr("老师id", "学生选择指导老师"));
			Student student = std.getById(id);
			student.setTeacher(tea.getById(tId));
			log.appendText("选择成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("选择失败\n");
		}
	}
	@FXML
	public void onDelTeacher(ActionEvent av) throws Exception {
		try {
			long id = Long.parseLong(getAttr("老师id", "删除老师"));
			Teacher teacher = tea.getById(id);
			tea.delete(teacher);
			log.appendText("删除成功\n");
		} catch (Exception e) {
			// TODO: handle exception
//			e.printStackTrace();
			log.appendText("删除失败\n");
		}
	}
	@FXML
	public void onStuModTeacher(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "学生修改指导教师"));
			Long tId = Long.parseLong(getAttr("老师id", "学生修改指导教师"));
			Student student = std.getById(id);
			student.setTeacher(tea.getById(tId));
			log.appendText("修改成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("修改失败\n");
		}
	}
	@FXML
	public void onSelStudent(ActionEvent av) throws Exception {
		try {
			Long tId = Long.parseLong(getAttr("老师id", "教师选择被指导的学生"));
			Long id = Long.parseLong(getAttr("学生id", "教师选择被指导的学生"));
			Student student = std.getById(id);
			Teacher Nteacher = tea.getById(tId);
			student.setTeacher(Nteacher);
			Nteacher.getStudents().add(student);
			log.appendText("选择成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("选择失败\n");
		}
	}
	@FXML
	public void onTeaModStudent(ActionEvent av) throws Exception {
		try {
			Long tId = Long.parseLong(getAttr("老师id", "教师修改被指导的学生"));
			Long id = Long.parseLong(getAttr("学生id", "教师修改被指导的学生"));
			Student student = std.getById(id);
			Teacher Nteacher = tea.getById(tId);
			student.setTeacher(Nteacher);
			Nteacher.getStudents().add(student);
			log.appendText("修改成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("修改失败\n");
		}
	}
}
